/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.brain.core.dto;

import com.brain.core.configuracion.WebServletContextListener;
import com.brain.core.view.mbean.ApplicationMBean;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.*;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;

/**
 *
 * @author Indra
 */
@Entity
@Table(name = "TCORE_ARCHIVO")
public class ArchivoDto extends ComunDto implements Serializable {

    private static final long serialVersionUID = 1L;
    // @Max(value=?)  @Min(value=?)//if you know range of your decimal fields consider using these annotations to enforce field validation
    @Id
    @GeneratedValue(strategy = GenerationType.TABLE, generator = "SEQ_TCORE_ARCHIVO")
    @TableGenerator(name = "SEQ_TCORE_ARCHIVO", table = "TCORE_ENTIDAD", pkColumnName = "NOMBRE", valueColumnName = "CORRELATIVO", pkColumnValue = "TCORE_ARCHIVO", allocationSize = 1)
    private Long id;
    @Column(name = "ESTADO")
    private Boolean estado;
    @Column(name = "ES_ATRIBUTO")
    private Boolean esAtributo;
    @Basic(optional = false)
    @Column(name = "ENTIDAD")
    private Long entidad;
    @JoinColumn(name = "TIPO", referencedColumnName = "ID")
    @ManyToOne
    private ParametroDto tipoDto;
    @Column(name = "EXTENSION")
    private String extension;
    @Basic(optional = false)
    @Column(name = "RUTA")
    private String ruta;
    @Basic(optional = false)
    @Column(name = "NOMBRE")
    private String nombre;
    @Basic(optional = false)
    @Column(name = "DETALLE")
    private String detalle;
    @Basic(optional = false)
    @Column(name = "PESO")
    private BigDecimal peso;
    @Basic(optional = false)
    @Column(name = "REGISTRO")
    private Long registro;
    @Transient
    private byte[] bites;
    @Transient
    private StreamedContent content;

    public ArchivoDto() {
    }

    public ArchivoDto(Long id) {
        this.id = id;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Boolean getEstado() {
        return estado;
    }

    public void setEstado(Boolean estado) {
        this.estado = estado;
    }

    public Long getEntidad() {
        return entidad;
    }

    public void setEntidad(Long entidad) {
        this.entidad = entidad;
    }

    public ParametroDto getTipoDto() {
        return tipoDto;
    }

    public void setTipoDto(ParametroDto tipoDto) {
        this.tipoDto = tipoDto;
    }

    public String getExtension() {
        return extension;
    }

    public void setExtension(String extension) {
        this.extension = extension;
    }

    public String getRuta() {
        return ruta;
    }

    public void setRuta(String ruta) {
        this.ruta = ruta;
    }

    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    public String getDetalle() {
        return detalle;
    }

    public void setDetalle(String detalle) {
        this.detalle = detalle;
    }

    public BigDecimal getPeso() {
        return peso;
    }

    public void setPeso(BigDecimal peso) {
        this.peso = peso;
    }

    public Long getRegistro() {
        return registro;
    }

    public void setRegistro(Long registro) {
        this.registro = registro;
    }

    public byte[] getBites() {
        return bites;
    }

    public void setBites(byte[] bites) {
        this.bites = bites;
    }

    public StreamedContent getContent() {


        StreamedContent contenido = null;

        if (bites == null) {
            ApplicationMBean applicationMBean = (ApplicationMBean) WebServletContextListener.getApplicationContext().getBean("applicationMBean");
            File file = new File(applicationMBean.getRutaArchivos() + this.nombre);
            if (file.isFile()) {
                try {
                    InputStream is = new FileInputStream(file);
                    contenido = new DefaultStreamedContent(is, "", this.nombre);
                    return contenido;
                } catch (FileNotFoundException ex) {
                    System.out.println(ex);
                    return null;
                }
            } else {
                return null;
            }
        }else {
            InputStream is = new ByteArrayInputStream(bites);        
            return new DefaultStreamedContent(is, "", this.nombre);
        }


    }

    public void setContent(StreamedContent content) {
        this.content = content;
    }

    public Boolean getEsAtributo() {
        return esAtributo;
    }

    public void setEsAtributo(Boolean esAtributo) {
        this.esAtributo = esAtributo;
    }
    
    

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof ArchivoDto)) {
            return false;
        }
        ArchivoDto other = (ArchivoDto) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.brain.core.dto.ArchivoDto[ id=" + id + " ]";
    }
}
